package com.cai310.lottery.ticket.protocol.local.disassemble;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cai310.lottery.service.lottery.jczq.JczqMatchEntityManager;
import com.cai310.lottery.support.UnitsCountUtils;
import com.cai310.lottery.support.jczq.JczqMatchItem;
import com.cai310.lottery.support.jczq.JczqUtil;
import com.cai310.lottery.support.jczq.PassMode;
import com.cai310.lottery.support.jczq.PassType;
import com.cai310.lottery.support.jczq.PlayType;
import com.cai310.lottery.support.jczq.SchemeType;
import com.cai310.lottery.support.jczq.TicketItem;
import com.cai310.lottery.ticket.dto.JczqMatchDTO;
import com.cai310.lottery.ticket.dto.PrintInterfaceDTO;
import com.cai310.lottery.ticket.dto.TicketDTO;
import com.cai310.lottery.ticket.protocol.local.dataconver.JczqContentConver;
import com.cai310.lottery.ticket.protocol.local.disassemble.jczq.JczqPrintItemObj;
import com.cai310.utils.DateUtil;
import com.cai310.utils.JsonUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

public class JczqDisassemble extends AbstractDisassemble {
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Override
	protected int getMaxMultiple() {
		return 99;
	}

	@Override
	protected int getMaxUnitsKeno() {
		return 5;
	}

	@Override
	protected int getMaxUnitsCommon() {
		return 5;
	}

	@Override
	protected List<TicketDTO> doCompound(PrintInterfaceDTO printData, Date createTime)
			throws Exception {
		List<TicketDTO> resultList = new ArrayList<TicketDTO>();
		PlayType playType = PlayType.values()[printData.getBetType()];
		
		Map<String, Object> map = JsonUtil.getMap4Json(printData.getContent());
		String ticketContent = String.valueOf(map.get("ticketContent"));
		PassMode passMode = PassMode.values()[Integer.valueOf(String.valueOf(map.get("passModeOrdinal")))];
		SchemeType schemeType = SchemeType.values()[Integer.valueOf(String.valueOf(map.get("schemeTypeOrdinal")))];
		List<TicketItem> ticketItemList = JczqUtil.getTicketList(ticketContent);
		Map<String, Object> content = JsonUtil.getMap4Json(String.valueOf(map.get("content")));
		String[] items = JsonUtil.getStringArray4Json(String.valueOf(content.get("items")));
		final List<JczqMatchItem> correctList = new ArrayList<JczqMatchItem>();
		for (String itemStr : items) {
			map = JsonUtil.getMap4Json(itemStr);
			JczqMatchItem item = new JczqMatchItem();
			item.setMatchKey(String.valueOf(map.get("matchKey")));
			item.setDan(Boolean.valueOf(String.valueOf(map.get("dan"))));
			item.setValue(Integer.valueOf(String.valueOf(map.get("value"))).intValue());
			correctList.add(item);
		}
		TicketDTO ticketNew = null;		
		for (TicketItem ticketItem : ticketItemList) {
			PassType passType = ticketItem.getPassType();
			int units = 0;
			List<JczqMatchItem> selectList = JczqUtil.getSelectByTicketItem(ticketItem,correctList);
			//计算注数
			for(int passMatch:passType.getPassMatchs()){
				units+=UnitsCountUtils.countUnits(passMatch, new ArrayList<JczqMatchItem>(), selectList);
			}
			//组合新单
			ticketNew = new TicketDTO();
			PropertyUtils.copyProperties(ticketNew, printData);
			ticketNew.setId(null);
			List<String> matchKeyList = Lists.newArrayList();
			for (JczqMatchItem item : selectList) {
				matchKeyList.add(item.getMatchKey());
			}
			JczqPrintItemObj jczqPrintItemObj = new JczqPrintItemObj();
			jczqPrintItemObj.setFirstEndTime(DateUtil.dateToStr(printData.getOfficialEndTime(), "yyyy-MM-dd HH:mm:ss"));
			jczqPrintItemObj.setBetUnits(units);
			jczqPrintItemObj.setItems(JczqContentConver.itemsToStrCompound(selectList, playType,passType,printData.getOfficialEndTime()));
			jczqPrintItemObj.setMultiple(ticketItem.getMultiple());
			jczqPrintItemObj.setTicketIndex(ticketItemList.indexOf(ticketItem));
			jczqPrintItemObj.setPassType(passType.ordinal());
			jczqPrintItemObj.setPassModeOrdinal(passMode.ordinal());
			jczqPrintItemObj.setSchemeTypeOrdinal(schemeType.ordinal());
			jczqPrintItemObj.setPlayTypeOrdinal(playType.ordinal());
			jczqPrintItemObj.setPassType(passType.ordinal());
			String printContent = JsonUtil.getJsonString4JavaPOJO(jczqPrintItemObj);
			ticketNew.setTicketIndex(ticketItemList.indexOf(ticketItem));
			ticketNew.setBetType(Byte.valueOf(String.valueOf(playType.ordinal())));
			ticketNew.setContent(printContent);
			ticketNew.setMultiple(ticketItem.getMultiple());
			ticketNew.setUnits(units);
			ticketNew.setSchemeCost(units * unitsMoney * ticketItem.getMultiple());
			ticketNew.setPrintinterfaceId(printData.getId());
			ticketNew.setCreateTime(createTime);
			resultList.add(ticketNew);///因为竞彩已经拆好了票
		}
		return resultList;
	}
	
}
